package com.rzico.sbl.ui.statistic

import android.widget.ImageView
import androidx.core.view.isVisible
import com.rzico.sbl.R
import com.rzico.sbl.databinding.ActivityShareBinding
import com.rzico.sbl.model.ShareWorkerData
import com.rzico.sbl.other.*
import com.rzico.sbl.viewmodel.StatisticCommonViewModel
import com.xinnuo.common.extend.*
import com.xinnuo.common.decoration.SpaceTBDecoration
import com.xinnuo.common.helper.phoneReplaceWithStar
import com.xinnuo.common_ui.base.BaseActivity
import com.xinnuo.common_ui.base.addItems
import com.xinnuo.common_ui.base.loadLinear
import com.xinnuo.common_ui.glideExt.loadCircleImage
import com.xinnuo.common_ui.utils.showTimeRangeDialog
import com.xinnuo.slimadapter.SlimAdapter

class ShareActivity : BaseActivity(R.layout.activity_share) {

    private val mBinding by lazy { ActivityShareBinding.bind(getRootView()) }

    private var mDateMode = "4"
    private var mBeginDate = getFirstDate()
    private var mEndDate = getEndDate()

    override fun getViewModel() = get<StatisticCommonViewModel>()

    override fun initData() {
        initTitle(title = "推广统计")

        initLayout()

        mBinding
            .listLayout
            .swipeRefresh
            .isRefreshing = true

        getData(pageNum)
    }

    private fun initLayout() {
        mBinding.shareBottom.gone()

        mBinding
            .listLayout
            .apply {
                emptyLayout.also {
                    it.emptyHint.text = "暂无相关推广记录！"
                }

                recycleList.also { view ->
                    view.loadLinear(
                        refresh = swipeRefresh,
                        onRefresh = { getData(0) },
                        onLoad = {
                            isLoadingMore
                                .conditionFalse {
                                    isLoadingMore = true
                                    getData(pageNum)
                                }
                        }
                    )

                    view.addItemDecoration(SpaceTBDecoration(hasHeader = false))

                    mAdapter = SlimAdapter.creator()
                        .register<ShareWorkerData>(R.layout.item_statistic_share) { jector, bean, _ ->
                            jector.apply {
                                text(R.id.item_share_name, bean.name.orEmpty("暂无"))
                                text(R.id.item_share_shop, bean.shopName.orEmpty())
                                text(
                                    R.id.item_share_phone,
                                    when {
                                        userRole in "1,2"
                                                || privacyStatus == "off" -> bean.mobile.orEmpty()
                                        else -> bean.mobile.phoneReplaceWithStar()
                                    }
                                )
                                text(R.id.item_share_num, bean.promoter)
                                text(R.id.item_share_amount, bean.amount.formatDecimal())
                                text(R.id.item_share_total, bean.quantity)

                                with<ImageView>(R.id.item_share_img) {
                                    it.loadCircleImage(bean.logo, R.mipmap.default_user)
                                }

                                clicked(R.id.item_share_num_ll) {
                                    startActivityEx<ShareRecordActivity>(
                                        "adminId" to bean.id,
                                        "dateMode" to mDateMode,
                                        "beginDate" to mBeginDate,
                                        "endDate" to mEndDate
                                    )
                                }

                                clicked(R.id.item_share_amount_ll) {
                                    startActivityEx<ShareOrderActivity>(
                                        "adminId" to bean.id,
                                        "dateMode" to mDateMode,
                                        "beginDate" to mBeginDate,
                                        "endDate" to mEndDate
                                    )
                                }

                                clicked(R.id.item_share_total_ll) {
                                    startActivityEx<ShareGoodsActivity>(
                                        "adminId" to bean.id,
                                        "dateMode" to mDateMode,
                                        "beginDate" to mBeginDate,
                                        "endDate" to mEndDate
                                    )
                                }
                            }
                        }
                        .attachTo(view)
                }
            }
    }

    override fun initListener() {
        mBinding
            .shareTime
            .apply {
                getViewModel().mUncheckTimeList[3].isChecked = true
                timeHint.text = getViewModel().mUncheckTimeList[3].hint

                timeLayout.oneClick {
                    getViewModel()
                        .showDropTimeFilter(
                            getIContext(),
                            timeHint,
                            timeHintArrow,
                            timeLayout,
                            getViewModel().mUncheckTimeList
                        ) { bean ->

                            (bean.id != "7")
                                .condition {
                                    getViewModel()
                                        .mUncheckTimeList
                                        .last()
                                        .name = "自定义时间"
                                }

                            when (bean.id) {
                                "1" -> {
                                    mDateMode = "1"
                                    mBeginDate = getTodayDate()
                                    mEndDate = getTodayDate()

                                    updateList()
                                }
                                "2" -> {
                                    mDateMode = "2"
                                    mBeginDate = getBeforeDate()
                                    mEndDate = getBeforeDate()

                                    updateList()
                                }
                                "3" -> {
                                    mDateMode = "3"
                                    mBeginDate = getMondayDate()
                                    mEndDate = getSundayDate()

                                    updateList()
                                }
                                "4" -> {
                                    mDateMode = "4"
                                    mBeginDate = getFirstDate()
                                    mEndDate = getEndDate()

                                    updateList()
                                }
                                "5" -> {
                                    mDateMode = "5"
                                    mBeginDate = getBeforeFirstDate()
                                    mEndDate = getBeforeEndDate()

                                    updateList()
                                }
                                "6" -> {
                                    mDateMode = "6"
                                    mBeginDate = getYearFirstDate()
                                    mEndDate = getYearEndDate()

                                    updateList()
                                }
                                "7" -> {
                                    showTimeRangeDialog(
                                        isCurrentDate = true,
                                        isAfterLimited = true,
                                        onResult = { start, end ->
                                            getViewModel()
                                                .mUncheckTimeList
                                                .forEach { it.isChecked = false }

                                            getViewModel()
                                                .mUncheckTimeList
                                                .last()
                                                .isChecked = true

                                            mDateMode = "7"
                                            mBeginDate = start
                                            mEndDate = end
                                            bean.name = "$mBeginDate ~ $mEndDate"
                                            timeHint.text = bean.name

                                            updateList()
                                        }
                                    )
                                }
                            }
                        }
                }
            }
    }

    /**
     * 推广统计一级列表
     */
    override fun getData(index: Int, isLoading: Boolean) {
        getViewModel()
            .shareList(
                page = index,
                beginDate = mBeginDate,
                endDate = mEndDate,
                onSuccess = {
                    mList.apply {
                        (index == 0)
                            .condition {
                                clear()
                                pageNum = 0
                            }

                        addItems(it)
                        it.notEmpty { pageNum++ }
                    }

                    mAdapter.setDataList(mList)
                },
                onFinally = {
                    isLoadingMore = false

                    mBinding
                        .listLayout
                        .apply {
                            swipeRefresh.isRefreshing = false

                            emptyLayout
                                .emptyView
                                .isVisible = mList.isEmpty()
                        }
                }
            )
    }

    /**
     * 刷新列表数据
     */
    private fun updateList() {
        mBinding
            .listLayout
            .apply {
                swipeRefresh.isRefreshing = true

                emptyLayout
                    .emptyView
                    .gone()
            }

        mList.notEmpty {
            mList.clear()
            mAdapter.notifyDataSetChanged()
        }

        getData(0)
    }

}